package xyz.robinbingo.leetcode.a20.b05.c11;

public class Solution1 {
    public double myPow(double x, int n) {
        if (x == 0 && n == 0) {
            throw new RuntimeException("error");
        }
        if (x == 0) {
            return 0;
        }
        if (n == 0) {
            return 1;
        }
        if (n == 1) {
            return x;
        }
        if (n == -1) {
            return 1 / x;
        }
        if (n % 2 == 0) {
            return myPow(x, n / 2) * myPow(x, n / 2);
        } else {
            if (n > 0) {
                return myPow(x, n / 2) * myPow(x, n / 2) * myPow(x, 1);
            }
            return myPow(x, n / 2) * myPow(x, n / 2) * myPow(x, -1);
        }
    }
}
